package com.itboye.hutouben.responsitory;

import com.google.gson.reflect.TypeToken;
import com.itboye.hutouben.bean.GouWuCheBean;
import com.itboye.hutouben.bean.OrderBeans;
import com.itboye.hutouben.interfaces.IOrderface;
import com.itboye.hutouben.volley.BaseErrorListener;
import com.itboye.hutouben.volley.BaseNetRepository;
import com.itboye.hutouben.volley.BaseSuccessReqListener;
import com.itboye.hutouben.volley.ByJsonRequest;
import com.itboye.hutouben.volley.ICompleteListener;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by itboye on 2016/12/9.
 */

public class OrderResponsitory extends BaseNetRepository implements IOrderface<OrderBeans> {
    public static final String Order = "By_Order_query";
    public static final String gouwuche = "By_ShoppingCart_query";//查询购物车
    public static final String delete = "By_ShoppingCart_delete";//购物车删除
    public static final String update = "By_ShoppingCart_update";//购物车更新
    public static final String querenShouHuo = "By_Order_receiveGoods";//确认收货
    public static final String quxiao = "By_Order_cancel";//取消订单

    public OrderResponsitory(ICompleteListener ICompleteListener) {
        super(ICompleteListener);
    }

    @Override
    public void onOrderPayments(String uid, String query_status, String page_index, String page_size) {

        Type type = new TypeToken<OrderBeans>() {
        }.getType();
//        String apiVer = api;
        Map<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        map.put("query_status", query_status);
        map.put("page_index", page_index);
        map.put("page_size", page_size);

        (new ByJsonRequest.Builder<OrderBeans>())
                .setTypeVerParamsAndReturnClass(Order, "100", map, type)
                .requestListener(
                        new BaseSuccessReqListener<OrderBeans>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    /**
     * 购物车
     *
     * @param uid
     */
    @Override
    public void gouWuChe(String uid) {
        Type type = new TypeToken<List<GouWuCheBean>>() {
        }.getType();
        String apiVer = "102";
        Map<String, Object> map = new HashMap<>();
        map.put("uid", uid);

        (new ByJsonRequest.Builder<List<GouWuCheBean>>())
                .setTypeVerParamsAndReturnClass(gouwuche, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<List<GouWuCheBean>>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    /**
     * 购物车删除
     *
     * @param uid
     * @param id
     */
    @Override
    public void gouWuCheShanChu(String uid, String id) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "100";
        Map<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        map.put("id", id);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(delete, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    /**
     * 购物车更新
     *
     * @param uid   添加人uid
     * @param id    购物车项ID
     * @param count 更新后的数量
     */
    @Override
    public void updateGouWuChe(String uid, String id, String count) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "100";
        Map<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        map.put("id", id);
        map.put("count", count);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(update, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    @Override
    public void querenShouHuo(String sid, String uid, String orderCode) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "101";
        Map<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        map.put("s_id", sid);
        map.put("order_code", orderCode);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(querenShouHuo, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }

    @Override
    public void quxiaoDingDan(String uid, String order_code) {
        Type type = new TypeToken<String>() {
        }.getType();
        String apiVer = "101";
        Map<String, Object> map = new HashMap<>();
        map.put("uid", uid);
        map.put("order_code", order_code);
        (new ByJsonRequest.Builder<String>())
                .setTypeVerParamsAndReturnClass(quxiao, apiVer, map, type)
                .requestListener(
                        new BaseSuccessReqListener<String>(
                                getListener()))
                .errorListener(new BaseErrorListener(getListener()))
                .desEncodeThenBuildAndSend();
    }
}
